1 using UnityEngine;
2 using
System.Collections;
3 using
UnityEngine.UI;
4
5 public
class PlayerHealth : MonoBehaviour {
6
7     
public int startingHealth = 100;
8     
public int currentHealth = 100;
9     
public Text healthText;
10     
public Slider healthSlider;
11
12     
float shakingTimer = 0;
13     
public float timeToShake = 1.0f;
14     
public float shakeIntensity = 3.0f;
15     
bool isShaking = false;
16
17     
bool isDead = false;
18     Animator anim;
19
20     
// Use this for initialization
21     
void Start () {
22         currentHealth = startingHealth;
23         anim = GetComponent<Animator> ();
24         healthSlider.
value = startingHealth;
25     }
26
27     
// Update is called once per frame
28     
void Update () {
29         
//healthText.text = "HP: " + currentHealth.ToString ();
30         healthText.text =
"HP:";
31         healthSlider.
value = currentHealth;
32
33         
if (isShaking == true && shakingTimer < timeToShake) {
34             shakingTimer += Time.deltaTime;
35             
float x = Mathf.PerlinNoise (Camera.main.transform.position.x, Camera.main.transform.position.y);
36             Camera.main.transform.position =
new Vector3 (Camera.main.transform.position.x + x * shakeIntensity, Camera.main.transform.position.y, Camera.main.transform.position.z);
37
38             
if (shakingTimer > timeToShake) {
39                 isShaking =
false;
40             }
41         }
42
43     }
44
45
46     
public void TakeDamage(int amount){
47         
if (isDead)
48             
return;
49
50         ShakeCamera ();
51
52         currentHealth -= amount;
53         
if (currentHealth <= 0) {
54             Death ();
55         }
56     }
57
58     
public void Death(){
59         
if (isDead)
60             
return;
61             
62         anim.SetTrigger (
"Death");
63         
//agent.enabled = false;
64         isDead =
true;
65         currentHealth =
0;
66         
//Destroy (gameObject, 2.5f);
67     }
68
69     
void ShakeCamera(){
70         shakingTimer =
0;
71         isShaking =
true;
72         
73     }
74         
75 }


Gõ tìm kiếm nhanh...